package com.example.wangzh.developkit.control;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.adapter.EvaluateAdapter;
import com.example.wangzh.developkit.base.BaseActivity;
import com.example.wangzh.developkit.model.Evaluate;
import com.example.wangzh.developkit.utils.UIUtils;
import com.example.wangzh.developkit.widget.CircleImageView;
import com.example.wangzh.developkit.widget.RatingBar;
import com.example.wangzh.developkit.widget.TopTitleBar;
import com.example.wangzh.developkit.widget.flowTagLayout.TagCloudLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by USER on 2017/5/25.
 */

public class RateActivity extends BaseActivity {
    @BindView(R.id.topTitleBar)
    TopTitleBar topTitleBar;
    @BindView(R.id.circleImageView)
    CircleImageView circleImageView;
    @BindView(R.id.txt_drive_name)
    TextView txt_drive_name;
    @BindView(R.id.txt_available_heavy)
    TextView txt_available_heavy;
    @BindView(R.id.txt_model)
    TextView txt_model;
    @BindView(R.id.txt_cahePlate)
    TextView txt_cahePlate;
    @BindView(R.id.ratingBar)
    RatingBar ratingBar;
    @BindView(R.id.txt_rate)
    TextView txt_rate;
    @BindView(R.id.txt_evaluate_count)
    TextView txt_evaluate_count;
    @BindView(R.id.ratingBar1)
    RatingBar ratingBar1;
    @BindView(R.id.txt_rate_1)
    TextView txt_rate_1;
    @BindView(R.id.tagCloudLayout)
    TagCloudLayout tagCloudLayout;
    @BindView(R.id.edt)
    EditText edt;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.img_contact_driver)
    ImageView img_contact_driver;
    @BindView(R.id.img_complain_driver)
    ImageView img_complain_driver;
    List<List<Evaluate>> evaluateLists;
    private List<Evaluate> crEvaluates;
    private EvaluateAdapter evaluateAdapter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_rate);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_rate;
    }


    protected void initData() {
    }

    @Override
    protected void initView() {
        crEvaluates = new ArrayList<>();
        evaluateAdapter = new EvaluateAdapter(this, crEvaluates);
        tagCloudLayout.setAdapter(evaluateAdapter);
        img_contact_driver.setOnClickListener(this);
        img_complain_driver.setOnClickListener(this);
        btn.setOnClickListener(this);
        getStarWithDetail();

    }

    private void getStarWithDetail() {
        List<Evaluate> e0 = Arrays.asList(new Evaluate[]{new Evaluate("严重超时", 0),
                new Evaluate("货物严重损坏", 0), new Evaluate("态度差", 0)});
        List<Evaluate> e1 = Arrays.asList(new Evaluate[]{new Evaluate("不准时", 1),
                new Evaluate("货物损坏", 1)});
        List<Evaluate> e2 = Arrays.asList(new Evaluate[]{new Evaluate("不准时", 2),
                new Evaluate("货物轻微损坏", 2)});
        List<Evaluate> e3 = Arrays.asList(new Evaluate[]{new Evaluate("货物完好", 3)});
        List<Evaluate> e4 = Arrays.asList(new Evaluate[]{new Evaluate("准时送达", 4),
                new Evaluate("货物完好", 4), new Evaluate("送到指定地点", 4),
                new Evaluate("服务态度非常好", 4), new Evaluate("司机驾驶技术好", 4),
                new Evaluate("值得信赖", 4)});
        evaluateLists = new ArrayList<>();
        evaluateLists.add(e0);
        evaluateLists.add(e1);
        evaluateLists.add(e2);
        evaluateLists.add(e3);
        evaluateLists.add(e4);
        refreshStar();
    }

    private void refreshStar() {
        refreshRate(-1);
        refreshTabCloud(-1);
        ratingBar1.setOnRateChangeListener(new RatingBar.OnRateChangeListener() {
            @Override
            public void onRateChange(int big, float small) {
                if (big > 4) {
                    big = 4;
                }
                if (big < 0) {
                    big = 0;
                }
                refreshTabCloud(big);
                refreshRate(big);
            }
        });
        tagCloudLayout.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
            @Override
            public void itemClick(View v, int position) {
                crEvaluates.get(position).setSelected(!crEvaluates.get(position).isSelected());
            }

        });
    }

    private void refreshRate(int big) {
        txt_rate_1.setText(big + 1 + "分");
    }

    private void refreshTabCloud(int big) {
        if (big < 0) {
            crEvaluates.clear();
            evaluateAdapter.notifyDataSetChanged();
            return;
        }
        List<Evaluate> evaluates = evaluateLists.get(big);
        crEvaluates.clear();
        crEvaluates.addAll(evaluates);
        evaluateAdapter.notifyDataSetChanged();
    }

    @Override
    protected void click(View view) {
        switch (view.getId()) {
            case R.id.btn:
                submitEvaluate();
            default:
                break;
        }
    }

    private void submitEvaluate() {
        if (ratingBar1.getRatingInt() == 0) {
            UIUtils.showToast("请评价司机");
            return;
        }
        UIUtils.showToast(getEvaluateStr());

    }

    private String getEvaluateStr() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < crEvaluates.size(); i++) {
            if (crEvaluates.get(i).isSelected()) {
                sb.append(crEvaluates.get(i).getEvalDetail() + ",");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        if (sb.length() > 0) {
            return ratingBar1.getRatingInt() + "星:" + sb.toString();
        } else {
            return ratingBar1.getRatingInt() + "星";
        }
    }
}
